package com.auction.system.action.auction;


import com.auction.system.base.BaseResponse;
import com.auction.system.domain.dto.auction.OrderInfo;
import com.auction.system.domain.query.auction.OrderInfoQuery;
import com.auction.system.enums.EnumOrderStatus;
import com.auction.system.enums.EnumReturnResult;
import com.auction.system.service.auction.IOrderInfoService;
import com.auction.system.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName OrderInfoAction
 * @Author 王伟学
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/orderInfo")
public class OrderInfoAction {

    @Autowired
    private IOrderInfoService orderInfoService;

    /**
     * 进入订单信息列表页面(所有订单)
     * @return
     */
    @RequestMapping(value = "/allList",method = RequestMethod.GET)
    public String allList(){
        return "orderInfo/allList";
    }

    /**
     * 进入订单信息列表页面(买方订单)
     * @return
     */
    @RequestMapping(value = "/buyerList",method = RequestMethod.GET)
    public ModelAndView buyerList(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("buyerId", UserUtil.getUserId());
        mav.setViewName("orderInfo/buyerList");
        return mav;
    }

    /**
     * 进入订单评价
     * @return
     */
    @RequestMapping(value = "/viewEvaluate",method = RequestMethod.GET)
    public ModelAndView viewEvaluate(Long id){
        ModelAndView mav = new ModelAndView();
        mav.addObject("orderId", id);
        mav.setViewName("orderInfo/viewEvaluate");
        return mav;
    }

    /**
     * 进入订单评价
     * @return
     */
    @RequestMapping(value = "/viewComplaint",method = RequestMethod.GET)
    public ModelAndView viewComplaint(Long id){
        ModelAndView mav = new ModelAndView();
        mav.addObject("orderId", id);
        mav.setViewName("orderInfo/viewComplaint");
        return mav;
    }

    /**
     * 进入订单信息列表页面(卖方订单)
     * @return
     */
    @RequestMapping(value = "/sellerList",method = RequestMethod.GET)
    public ModelAndView sellerList(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("sellerId", UserUtil.getUserId());
        mav.setViewName("orderInfo/sellerList");
        return mav;
    }

    /**
     * 获取订单信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public OrderInfoQuery getList(OrderInfoQuery query) {
        try {
            query = orderInfoService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取订单信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取订单信息数据异常");
        }
        return query;
    }

    /**
     * 获取订单信息数据（买方）
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getBuyList",method = RequestMethod.POST)
    public OrderInfoQuery getBuyList(OrderInfoQuery query) {
        try {
            query.setBuyerId(Long.parseLong(UserUtil.getUserId()));
            query = orderInfoService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取订单信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取订单信息数据异常");
        }
        return query;
    }

    /**
     * 获取订单信息数据（卖方）
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSellerList",method = RequestMethod.POST)
    public OrderInfoQuery getSellerList(OrderInfoQuery query) {
        try {
            query.setSellerId(Long.parseLong(UserUtil.getUserId()));
            query = orderInfoService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取订单信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取订单信息数据异常");
        }
        return query;
    }
    /**
     * 已支付（卖家确认买方已支付）
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pay",method = RequestMethod.POST)
    public BaseResponse pay(Long id){
        BaseResponse response = new BaseResponse();
        OrderInfo orderInfo = orderInfoService.get(id);
        try{
            //如果状态为已送达，则交易完成；状态为待线下交易，则已支付
            if(EnumOrderStatus.SEND_SUCCESS.getCode().equals(orderInfo.getOrderStatus())){
                orderInfo.setOrderStatus(EnumOrderStatus.TRADE_SUCCESS.getCode());
            }else{
                orderInfo.setOrderStatus(EnumOrderStatus.PAY_SUCCESS.getCode());
            }
            orderInfoService.edit(orderInfo);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("操作成功,订单状态为:"+EnumOrderStatus.getName(orderInfo.getOrderStatus()));
        }catch (Exception e){
            log.info("操作已支付异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("操作失败,订单状态为:"+EnumOrderStatus.getName(orderInfo.getOrderStatus()));
        }
        return response;
    }

    /**
     * 已送达（买家确认卖方已送达）
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/send",method = RequestMethod.POST)
    public BaseResponse send(Long id){
        BaseResponse response = new BaseResponse();
        OrderInfo orderInfo = orderInfoService.get(id);
        try{
            //如果状态为已支付，则交易完成；状态为待线下交易，则已送达
            if(EnumOrderStatus.PAY_SUCCESS.getCode().equals(orderInfo.getOrderStatus())){
                orderInfo.setOrderStatus(EnumOrderStatus.TRADE_SUCCESS.getCode());
            }else{
                orderInfo.setOrderStatus(EnumOrderStatus.SEND_SUCCESS.getCode());
            }
            orderInfoService.edit(orderInfo);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("操作成功,订单状态为:"+EnumOrderStatus.getName(orderInfo.getOrderStatus()));
        }catch (Exception e){
            log.info("操作已送达异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("操作失败,订单状态为:"+EnumOrderStatus.getName(orderInfo.getOrderStatus()));
        }
        return response;
    }
}
